﻿using AppFramework.Admin.HandyUI.Themes.Controls;
using AppFramework.Admin.Services;
using AppFramework.Shared;
using AppFramework.Shared.Services;
using AppFramework.Shared.Services.App;
using AppFramework.Shared.Services.Mapper;
using Hardcodet.Wpf.TaskbarNotification;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Regions; 
using System.Windows; 

namespace AppFramework.Admin.HandyUI
{
    //继承PrismPrismApplication Prism框架的基类
    public partial class App : PrismApplication, IAppTaskBar
    {
        private TaskbarIcon? taskBar;

        protected override Window? CreateShell() => null;

        /// <summary>
        /// 重写基类的方法  初始化成功事件
        /// </summary>
        protected override async void OnInitialized()
        {
            //初始化
            Initialization();
            //使用PrismIoc框架

            //检查软件是否有新版本
            var appVersionService = ContainerLocator.Container.Resolve<IUpdateService>();
            await appVersionService.CheckVersion();
            //注入HandyUIStartService服务（跟实例化的作用一样）
            var appStart = ContainerLocator.Container.Resolve<IAppStartService>();
            //创建主窗口
            appStart.CreateShell();

            base.OnInitialized();
        }

        /// <summary>
        /// 注册应用程序中的所有类型  （容器使用的是Prism.DryIOC这个容器）
        /// </summary>
        /// <param name="container"></param>
        protected override void RegisterTypes(IContainerRegistry container)
        {
            //注册视图和视图模型
            container.AddViews();
            //注册AppFramework.Admin层的服务
            container.AddAdminsServices();
            //注册AppFramework.Shared层的服务
            container.AddSharedServices();

            //单例模式注册
            container.RegisterSingleton<IHostDialogService, DialogHostService>();
            container.RegisterSingleton<IAppStartService, HandyUIStartService>(); //注册 HandyUIStartService

            //域模式注册
            container.RegisterScoped<IPermissionTreesService, PermissionTreesService>();
            container.RegisterScoped<IFeaturesService, FeaturesService>();

            //注册本地化服务（多语言）
            container.RegisterSingleton<ILocaleCulture, LocaleCulture>(); 

            //注册 菜单服务（左侧的菜单配置）
            container.RegisterSingleton<INavigationMenuService, NavigationSingleMenuService>();
        }

        /// <summary>
        /// Prism 区域的注册
        /// </summary>
        /// <param name="regionAdapterMappings"></param>
        protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
        {
            regionAdapterMappings.RegisterMapping<TabControl, TabControlRegionAdapter>();
            base.ConfigureRegionAdapterMappings(regionAdapterMappings);
        }

        /// <summary>
        /// 初始化 任务栏小图标
        /// </summary>
        public void Initialization()
        {
            taskBar = (TaskbarIcon)FindResource("taskBar");
        }

        /// <summary>
        /// 释放内存  因为图标是非托管资源 需要手动释放  Dispose释放
        /// </summary>
        /// 
        //=>箭头函数 调用的时候会执行=>后面的代码
        public void Dispose() => taskBar?.Dispose();
    }
}
